package kr.hkit.powerjava.project_cafe;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTable;

import java.awt.BorderLayout;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.JScrollPane;

import java.awt.Font;

import javax.swing.BoxLayout;
import javax.swing.ScrollPaneConstants;

import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class Dialog_Output01 extends JDialog  {
	private JTable table;
	private static ProductDAO dao;
	
	private Panel_Input pProdId;
	private Panel_Input pProdName;
	private Panel_Input pProdPrice;
	private Panel_Input pProdSales;
	private Panel_Input pProdMargin;
	
	private JPanel panel_2;
	private String[][] dataLists;
	private JPanel panel_1;
	
	
	public static void main(String[] args) {
		try {
			Dialog_Output01 dialog = new Dialog_Output01(dao);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * Create the panel.
	 */
	
	
	public Dialog_Output01(ProductDAO dao) {
		super();
		this.dao = dao;
		initialize();
	}

	

	private void initialize() {
		
		setTitle("출력1 - 판매금액순위");
		getContentPane().setLayout(new BorderLayout(0, 0));
		setSize(800, 300);
		
		
		panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.NORTH);
		
		JLabel lblNewLabel_1 = new JLabel("판 매 금 액 순 위");
		panel_1.add(lblNewLabel_1);
		lblNewLabel_1.setFont(new Font("맑은 고딕", Font.BOLD, 18));
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		
		panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		loadData();
		
		
		JScrollPane scrollPane = new JScrollPane(table);
		panel_2.add(scrollPane, BorderLayout.CENTER);
		getContentPane().add(panel_2);
		
		scrollPane.setViewportView(table);
		
		tableCellAlginAndWidth_cafe();
		table.setAutoCreateRowSorter(true);
		
		JPanel panel_3 = new JPanel();
		getContentPane().add(panel_3, BorderLayout.SOUTH);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		panel_3.add(lblNewLabel_2);
	}

	private void loadData() {
		
		ArrayList<Product> listNameProd = new ArrayList<>(); 
		try {
			listNameProd = dao.getProdList();
		} catch (SQLException e) {
			System.out.println("쿼리오류");
			e.printStackTrace();
		}
		
		String[] columns = {"순위", "제품코드", "제품명", "제품단가", "판매수량", "공급가액", "부가세액", "판매금액", "마진율", "마진액"};
		dataLists = new String[listNameProd.size()][columns.length];
		DecimalFormat df = new DecimalFormat("#,###.#"); //포멧
		DecimalFormat df1 = new DecimalFormat("#,###"); //
		
		
		for (int i = 0; i < listNameProd.size(); i++) {
			Product temp = listNameProd.get(i);
			dataLists[i] = new String[] {temp.getRank()+"", temp.getProdId(), 
					temp.getProdName(), temp.getProdPrice()+"",
					temp.getProdSales()+"", df.format(temp.getSupply()),
					df.format(temp.getVat()), df1.format(temp.getSellingPrice()), 
					temp.getProdMargin()+"", df.format(temp.getMarginPrice())};
		}
		
		//System.out.println(dataLists); //이거널포인트
		System.out.println(listNameProd);
		
		
		DefaultTableModel model= new DefaultTableModel(dataLists, columns); //바꾸기
		table = new JTable(model);
		
		/*JScrollPane scrollPane = new JScrollPane(table);
		panel.add(scrollPane, BorderLayout.CENTER);*/
		
		//scrollPane.setViewportView(table);
		
		/*tableCellAlginAndWidth_emp();
		table_emp.setAutoCreateRowSorter(true);*/
		
	}

	//정렬
	
	public void tableCellAlginAndWidth_cafe(){
		
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		 TableColumnModel cModel = table.getColumnModel();
		 
		 cModel.getColumn(0).setCellRenderer(dtcrCenter);
		 cModel.getColumn(1).setCellRenderer(dtcrCenter);
		 cModel.getColumn(2).setCellRenderer(dtcrCenter);
		 cModel.getColumn(3).setCellRenderer(dtcrRight);
		 cModel.getColumn(4).setCellRenderer(dtcrRight);
		 cModel.getColumn(5).setCellRenderer(dtcrRight);
		 cModel.getColumn(6).setCellRenderer(dtcrRight);
		 cModel.getColumn(7).setCellRenderer(dtcrRight);
		 cModel.getColumn(8).setPreferredWidth(40);
		 cModel.getColumn(8).setCellRenderer(dtcrRight);
		 cModel.getColumn(9).setPreferredWidth(100);
		 cModel.getColumn(9).setCellRenderer(dtcrRight);
		
	}
	
	
		/*
	public Product getProdList() { //보고 안되면 지우기 
		
		Connection con = ConnectionFactory.getConnection();
		dao = new ProductDAO(con);
		
		
		int ranking = 1; //순위 (수정하기)
		String prodId = pProdId.getTf().getText();
		String prodName = pProdName.getTf().getText();
		int price = Integer.parseInt(pProdPrice.getTf().getText().trim()); //제품단가
		int sales = Integer.parseInt(pProdSales.getTf().getText().trim()); //판매수량
		int margin = Integer.parseInt(pProdMargin.getTf().getText().trim()); //마진율
		
		int sellingPrice = (price * sales); //판매금액
		double vat = (sellingPrice/11); //부가세액
		double supply = (sellingPrice - vat); //공급가액
		double marginPrice = (supply * margin) ; //마진액
		
		return new Product(ranking, prodId, prodName, 
				price, sales, supply, vat, 
				sellingPrice, margin, marginPrice);
		
		
	}*/
	
}
